using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using University.Models;

namespace University.Controllers
{   
    public class SubjectsController : Controller
    {
		private readonly ISubjectRepository subjectRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public SubjectsController() : this(new SubjectRepository())
        {
        }

        public SubjectsController(ISubjectRepository subjectRepository)
        {
			this.subjectRepository = subjectRepository;
        }

        //
        // GET: /Subjects/

        public ViewResult Index()
        {
            return View(subjectRepository.AllIncluding(subject => subject.ScheduleItems, subject => subject.Marks));
        }

        //
        // GET: /Subjects/Details/5

        public ViewResult Details(int id)
        {
            return View(subjectRepository.Find(id));
        }

        //
        // GET: /Subjects/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Subjects/Create

        [HttpPost]
        public ActionResult Create(Subject subject)
        {
            if (ModelState.IsValid) {
                subjectRepository.InsertOrUpdate(subject);
                subjectRepository.Save();
                return RedirectToAction("Index");
            } else {
				return View();
			}
        }
        
        //
        // GET: /Subjects/Edit/5
 
        public ActionResult Edit(int id)
        {
             return View(subjectRepository.Find(id));
        }

        //
        // POST: /Subjects/Edit/5

        [HttpPost]
        public ActionResult Edit(Subject subject)
        {
            if (ModelState.IsValid) {
                subjectRepository.InsertOrUpdate(subject);
                subjectRepository.Save();
                return RedirectToAction("Index");
            } else {
				return View();
			}
        }

        //
        // GET: /Subjects/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(subjectRepository.Find(id));
        }

        //
        // POST: /Subjects/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            subjectRepository.Delete(id);
            subjectRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                subjectRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

